//
//  WSPlayListViewController.m
//  GroupFourFM
//
//  Created by 王鑫 on 15/12/23.
//  Copyright © 2015年 GroupFour. All rights reserved.
//

#import "WSPlayListViewController.h"
#import "WSPlayListTableViewCell.h"
#import "WSTrackModel.h"
#import "WSFavourModel.h"
#import "WSPlayViewController.h"

@interface WSPlayListViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic,strong)WSBaseTableView *listTableView;

@end

@implementation WSPlayListViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.view bringSubviewToFront:self.messageView];
    self.messageView.center = CGPointMake(SCREEN_WIDTH/2,  SCREEN_HEIGHT *0.85);
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"播放列表";
    
    UIButton * backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 0 , 20, 30);
    backButton.backgroundColor = CLEAR;
    backButton.tintColor = BLACK;
    [backButton setImage:[[UIImage imageNamed:@"btn_back_n"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    [self createTableView];
}

#pragma mark - 创建tableView
-(void)createTableView
{
    
    self.listTableView = [[WSBaseTableView alloc] init];
    [self.view addSubview:self.listTableView];
    self.listTableView.dataSource = self;
    self.listTableView.delegate = self;
    self.listTableView.rowHeight = 65;
    [self.listTableView registerClass:[WSPlayListTableViewCell class] forCellReuseIdentifier:@"WSPlayListTableViewCell"];
    [self.view addSubview:self.listTableView];
    
    [self.listTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(@0);
    }];
}

- (void)backBtnAction:(UIButton *)button {

    [self.navigationController popViewControllerAnimated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _playArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    WSPlayListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WSPlayListTableViewCell"];
    WSTrackModel *model = [self.playArray objectAtIndex:indexPath.row];
    WSFavourModel *historyModel = [self.playArray objectAtIndex:indexPath.row];
    cell.newsTitle.text = model.title;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    if (indexPath.row == _indexRow) {
//        [cell.newsPic sd];
    }
    else {
        [cell.newsPic setImage:[UIImage imageNamed:@"sound_playbtn"]];
    }
    
    if ([_kind isEqualToString:@"播放历史"]) {
        [cell.newsPic sd_setImageWithURL:[NSURL URLWithString:historyModel.coverPath] placeholderImage:PLACEHOLDER_CELL_CIRCLE];
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    WSTrackModel *sing = [self.playArray objectAtIndex:indexPath.row];
    [self.delegate changePlayMusic:sing.trackId indexRow:indexPath.row];

    [self.navigationController popViewControllerAnimated:YES];
}

- (void)setPlayArray:(NSMutableArray *)playArray {

    _playArray = playArray;
    [self.listTableView reloadData];
}

#pragma mark - 返回按钮方法
-(void)backButtonAction:(UIButton *)btn
{
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
